package com.sokal.easycare.modules.product.web.catalog;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.product.entity.catalog.ProCatalog;
import com.sokal.easycare.modules.product.service.catalog.ProCatalogService;

/**
 * 产品目录Controller
 * @author liliangdong
 * @version 2017-03-12
 */
@Controller
@RequestMapping(value = "${adminPath}/product/catalog/proCatalog")
public class ProCatalogController extends BaseController {

	@Autowired
	private ProCatalogService proCatalogService;
	
	@ModelAttribute
	public ProCatalog get(@RequestParam(required=false) String id) {
		ProCatalog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = proCatalogService.get(id);
		}
		if (entity == null){
			entity = new ProCatalog();
		}
		return entity;
	}
	
	@RequiresPermissions("product:catalog:proCatalog:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProCatalog proCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProCatalog> list = proCatalogService.findList(proCatalog); 
		model.addAttribute("list", list);
		return "modules/product/catalog/proCatalogList";
	}

	@RequiresPermissions("product:catalog:proCatalog:view")
	@RequestMapping(value = "form")
	public String form(ProCatalog proCatalog, Model model) {
		if (proCatalog.getParent()!=null && StringUtils.isNotBlank(proCatalog.getParent().getId())){
			proCatalog.setParent(proCatalogService.get(proCatalog.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(proCatalog.getId())){
				ProCatalog proCatalogChild = new ProCatalog();
				proCatalogChild.setParent(new ProCatalog(proCatalog.getParent().getId()));
				List<ProCatalog> list = proCatalogService.findList(proCatalog); 
				if (list.size() > 0){
					proCatalog.setSort(list.get(list.size()-1).getSort());
					if (proCatalog.getSort() != null){
						proCatalog.setSort(proCatalog.getSort() + 30);
					}
				}
			}
		}
		if (proCatalog.getSort() == null){
			proCatalog.setSort(30);
		}
		model.addAttribute("proCatalog", proCatalog);
		return "modules/product/catalog/proCatalogForm";
	}

	@RequiresPermissions("product:catalog:proCatalog:edit")
	@RequestMapping(value = "save")
	public String save(ProCatalog proCatalog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proCatalog)){
			return form(proCatalog, model);
		}
		proCatalogService.save(proCatalog);
		addMessage(redirectAttributes, "保存产品目录成功");
		return "redirect:"+Global.getAdminPath()+"/product/catalog/proCatalog/?repage";
	}
	
	@RequiresPermissions("product:catalog:proCatalog:edit")
	@RequestMapping(value = "delete")
	public String delete(ProCatalog proCatalog, RedirectAttributes redirectAttributes) {
		proCatalogService.delete(proCatalog);
		addMessage(redirectAttributes, "删除产品目录成功");
		return "redirect:"+Global.getAdminPath()+"/product/catalog/proCatalog/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ProCatalog> list = proCatalogService.findList(new ProCatalog());
		for (int i=0; i<list.size(); i++){
			ProCatalog e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}